unit fMeshSimple;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.StdCtrls,
  Vcl.Forms,
  Vcl.Dialogs,

  GLS.VectorTypes,
  GLS.Objects,
  GLS.Scene,
  GLS.Color,
  GLS.VectorGeometry,
  GLS.Texture,
  GLS.Mesh,
  GLS.SceneViewer,
  GLS.Coordinates,
  GLS.State,
  GLS.PersistentClasses,
  GLS.BaseClasses;

type
  PGLMesh = ^TGLMesh;
  PGLCamera = ^TGLCamera;

  TForm1 = class(TForm)
    GLScene1: TGLScene;
    SceneViewer: TGLSceneViewer;
    GLCamera1: TGLCamera;
    GLSceneViewer2: TGLSceneViewer;
    GLSceneViewer3: TGLSceneViewer;
    GLSceneViewer1: TGLSceneViewer;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    GLLightSource1: TGLLightSource;
    Label5: TLabel;
    DummyCube1: TGLDummyCube;
    Mesh1: TGLMesh;
    GLScene2: TGLScene;
    GLLightSource2: TGLLightSource;
    DummyCube2: TGLDummyCube;
    Mesh2: TGLMesh;
    GLCamera2: TGLCamera;
    GLScene3: TGLScene;
    GLLightSource3: TGLLightSource;
    DummyCube3: TGLDummyCube;
    Mesh3: TGLMesh;
    GLCamera3: TGLCamera;
    GLScene4: TGLScene;
    GLLightSource4: TGLLightSource;
    DummyCube4: TGLDummyCube;
    Mesh4: TGLMesh;
    GLCamera4: TGLCamera;
    procedure FormCreate(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton3Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
    procedure GLSceneViewer2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure GLSceneViewer2MouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure GLSceneViewer3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
  private
    faCameras: array [0 .. 2] of PGLCamera;
    faMeshes: array [0 .. 2] of PGLMesh;
    mx, my: Integer;
    procedure BuildMesh(n: Integer; aColor: TGLColorVector);
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BuildMesh(n: Integer; aColor: TGLColorVector);
var
  Mesh: PGLMesh;
  v1, v2, v3: TAffineVector;
  Color: TGLColorVector;
  i: Integer;
begin
  Color := aColor;
  Mesh := faMeshes[n];
  Mesh^.Mode := mmTriangles;
  Mesh^.Vertices.Clear;

  // make a mesh
  for i := 0 to 3 do
  begin
    SetVector(v1, 5, -10 + i * 5, 5);
    SetVector(v2, -5, 5, i * 5 + 5);
    SetVector(v3, 5, -10 + i * 5, -10 + i * 5);
    Mesh^.Vertices.AddVertex(v1, NullVector, Color, NullTexPoint);
    Mesh^.Vertices.AddVertex(v2, NullVector, Color, NullTexPoint);
    Mesh^.Vertices.AddVertex(v3, NullVector, Color, NullTexPoint);
    SetVector(v3, 5, -10 + i * 5, 5);
    SetVector(v2, -5, 5, i * 5 + 5);
    SetVector(v1, 5, -10 + i * 5, -10 + i * 5);
    Mesh^.Vertices.AddVertex(v1, NullVector, Color, NullTexPoint);
    Mesh^.Vertices.AddVertex(v2, NullVector, Color, NullTexPoint);
    Mesh^.Vertices.AddVertex(v3, NullVector, Color, NullTexPoint);
  end;
  Mesh^.CalcNormals(fwCounterClockWise);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  faCameras[0] := @GLCamera2;
  faCameras[1] := @GLCamera3;
  faCameras[2] := @GLCamera4;
  faMeshes[0] := @Mesh2;
  faMeshes[1] := @Mesh3;
  faMeshes[2] := @Mesh4;
  BuildMesh(0, clrTurquoise);
  BuildMesh(1, clrOrange);
  BuildMesh(2, clrGreen);
end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  SceneViewer.Camera := faCameras[0]^;
  SceneViewer.Camera.TransformationChanged;
end;

procedure TForm1.RadioButton3Click(Sender: TObject);
begin
  SceneViewer.Camera := faCameras[2]^;
  SceneViewer.Camera.TransformationChanged;
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  SceneViewer.Camera := faCameras[1]^;
  SceneViewer.Camera.TransformationChanged;
end;

procedure TForm1.GLSceneViewer2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  SceneViewer.Camera := faCameras[0]^;
  SceneViewer.Camera.TransformationChanged;
end;

procedure TForm1.GLSceneViewer2MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  if ssLeft in Shift then
  begin
    TGLSceneViewer(Sender).Camera.MoveAroundTarget(my - Y, mx - X);
    my := Y;
    mx := X;
  end;
end;

procedure TForm1.GLSceneViewer3MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  SceneViewer.Camera := faCameras[1]^;
  SceneViewer.Camera.TransformationChanged;
end;

procedure TForm1.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  SceneViewer.Camera := faCameras[2]^;
  SceneViewer.Camera.TransformationChanged;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SceneViewer.Camera := GLCamera1;
end;

end.
